package com.mo.tools.verification.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @name: EnumValueValidator
 * @description:
 * @author: Yang Xingfu
 * @date: 2021-01-29 14:58
 **/
public class EnumValueValidator implements ConstraintValidator<EnumValue, Object> {

    private Object[] values = null;

    @Override
    public void initialize(EnumValue constraintAnnotation) {
        values = constraintAnnotation.value();
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        } else if (values != null) {
            for (int i = 0; i < values.length; i++) {
                if (String.valueOf(value).equals(String.valueOf(values[i]))) {
                    return true;
                }
            }
        }
        return false;
    }

}
